package com.order.utils;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.ddl.history.IDdlGenerator;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderUtils {

    private static final String BIZ_PREFIX = "EO";

    public static String generateOrderNo() {
        try {
            IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator(InetAddress.getLocalHost());
            Number id = identifierGenerator.nextId(new Object());
            // 2. 获取当前日期（年月日）
            String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
            // 3. 截取雪花ID的后8位作为序列号
            String sequence = String.valueOf(id).substring(10);
            // 4. 组合
            return BIZ_PREFIX + date + sequence;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String generateRefundOrderNo(){
        return "R" + generateOrderNo();
    }

    public static Long generatePaymentSerialNumber() {
        try {
            IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator(InetAddress.getLocalHost());
            Number id = identifierGenerator.nextId(new Object());
            return id.longValue();
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
